package com.gin.supermarket_inventory_system.inventory.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.gin.database.base.BasePo;
import com.gin.supermarket_inventory_system.inventory.enums.InventoryReceiptType;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Comment;

/**
 * 库存数量变动详情(单个库存的变动情况)
 * @since : 2023/10/10 17:53
 * @author : ginstone
 * @version : v1.0.0
 **/
@Getter
@Setter
@TableName(value = InventoryReceiptDetailPo.TABLE_NAME, autoResultMap = true)
@Entity(name = InventoryReceiptDetailPo.TABLE_NAME)
@NoArgsConstructor
@Table(indexes = {
        @Index(columnList = "receiptId"),
        @Index(columnList = "type,inventoryId"),
})
public class InventoryReceiptDetailPo extends BasePo {
    protected static final String TABLE_NAME = "tb_inventory_entity_receipt_detail";

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    @Comment("单据类型")
    InventoryReceiptType type;

    @Column(nullable = false)
    @Comment("单据ID")
    long receiptId;

    @Column(nullable = false)
    @Comment("库存id")
    long inventoryId;

    @Column(nullable = false)
    @Comment("数量或变动数量")
    int amount;

    @Column(nullable = false, scale = 2)
    @Comment("单价")
    double price;

    @Column(nullable = false, scale = 2)
    @Comment("总价")
    double totalPrice;

    @Column(length = 1000)
    @Comment("备注")
    String remark;


    /**
     * 统计用字段
     */
    @Transient
    @TableField(exist = false)
    String date;

    /**
     * 计算总价
     */
    public void calculateTotalPrice() {
        this.totalPrice = amount * price;
    }
}